package com.shujia;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.ArrayList;
import java.util.Properties;

public class Demo2Consumer {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "master:9092,node2:9092,node2:9092");
        properties.setProperty("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        /*
         * earliest
         * 当各分区下有已提交的offset时，从提交的offset开始消费；无提交的offset时，从头开始消费
         * latest  默认
         * 当各分区下有已提交的offset时，从提交的offset开始消费；无提交的offset时，消费新产认值生的该分区下的数据
         * none
         * topic各分区都存在已提交的offset时，从offset后开始消费；只要有一个分区不存在已提交的offset，则抛出异常
         *
         */
        properties.setProperty("auto.offset.reset", "earliest");
        properties.setProperty("group.id", "asdasdasdas");//同一条数据在一个组内只消费一次

        //创建消费者
        KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(properties);

        ArrayList<String> topics = new ArrayList<>();
        topics.add("bigdata");

        //订阅topic
        kafkaConsumer.subscribe(topics);

        while (true) {
            //拉取数据
            ConsumerRecords<String, String> consumerRecords = kafkaConsumer.poll(2000);

            //循环解析数据
            for (ConsumerRecord<String, String> consumerRecord : consumerRecords) {
                String topic = consumerRecord.topic();
                String key = consumerRecord.key();
                String value = consumerRecord.value();
                long offset = consumerRecord.offset();
                int partition = consumerRecord.partition();
                System.out.println(topic + "\t" + key + "\t" + value + '\t' + offset + "\t" + partition);
            }
        }


    }
}
